package com.smile.member.controller;

import com.smile.common.exception.BizCodeEnum;
import com.smile.common.utils.PageUtils;
import com.smile.common.utils.R;
import com.smile.member.entity.MemberEntity;
import com.smile.member.exception.PhoneExitException;
import com.smile.member.exception.UserExitException;
import com.smile.member.service.MemberService;
import com.smile.member.vo.MemberLoginVo;
import com.smile.member.vo.MemberOAuth2Vo;
import com.smile.member.vo.MemberRegisterVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Map;


/**
 * 会员
 *
 * @author smile
 * @email hongshengmo@163.com
 * @date 2022-03-25 13:35:59
 */
@RestController
@RequestMapping("member/member")
public class MemberController {
    @Autowired
    private MemberService memberService;

    /**
     * 社交账户登录
     */
     @PostMapping("/oauth2/login")
     public R oauthLogin(@RequestBody MemberOAuth2Vo memberOAuth2Vo){
         return R.ok();
     }

    /** 用户登录 */
    @PostMapping("/login")
    public R login(@RequestBody MemberLoginVo vo){
        MemberEntity entity = memberService.loginUser(vo);
        if(entity!=null) {
            return R.ok().setData(entity);
        }else {
            return R.error(BizCodeEnum.LOGINACCT_PASSWORD_EXCEPTION.getCode(),BizCodeEnum.LOGINACCT_PASSWORD_EXCEPTION.getMsg());
        }
    }

    /**
     * 用户注册
     */
    @PostMapping("/register")
    public R register(@RequestBody MemberRegisterVo Vo) {
        try {
            memberService.registerUser(Vo);
        }catch (PhoneExitException phoneExitException){
            return R.error(BizCodeEnum.PHONE_EXIST_EXCEPTION.getCode(),BizCodeEnum.PHONE_EXIST_EXCEPTION.getMsg());
        }catch (UserExitException userExitException){
            return R.error(BizCodeEnum.USER_EXIST_EXCEPTION.getCode(),BizCodeEnum.USER_EXIST_EXCEPTION.getMsg());
        }


        return R.ok();
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = memberService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id) {
        MemberEntity member = memberService.getById(id);

        return R.ok().put("member", member);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody MemberEntity member) {
        memberService.save(member);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody MemberEntity member) {
        memberService.updateById(member);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids) {
        memberService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
